<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class AllowOriginMiddleware
{

    public function handle(Request $request, Closure $next)
    {
        $response = $next($request);
        $origin = $request->server('HTTP_ORIGIN') ? $request->server('HTTP_ORIGIN') : '';
        // infoLog('origin ===== '.json_encode($request->server()));
        $allow_origin = [
            'http://localhost:8080',
            'http://henlius.mobile.netloop.cn',
            'http://henlius.netloop.cn'
        ];
        // infoLog('=== origin : ===' . $origin);

        if (in_array($origin, $allow_origin)) {
            // infoLog('Yes, it exists in the whitelist');
            $response->header('Access-Control-Allow-Origin', $origin);
            $response->header('Access-Control-Allow-Headers', 'Origin, Content-Type, Cookie, X-CSRF-TOKEN, Accept, Authorization, X-XSRF-TOKEN');
            $response->header('Access-Control-Expose-Headers', 'Authorization, authenticated');
            $response->header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, OPTIONS');
            $response->header('Access-Control-Allow-Credentials', 'true');
        }
        return $response;


        //         return $next($request)
        // ->header('Access-Control-Allow-Origin', '*')
        // ->header('Access-Control-Allow-Methods', '*')
        // ->header('Access-Control-Allow-Headers', '*')
        // ->header('Access-Control-Request-Headers', '*');


    }
}
